package com.jzo2o.customer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.ForbiddenOperationException;
import com.jzo2o.common.model.CurrentUserInfo;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.common.utils.StringUtils;
import com.jzo2o.customer.enums.CertificationStatusEnum;
import com.jzo2o.customer.mapper.AgencyCertificationAuditMapper;
import com.jzo2o.customer.mapper.AgencyCertificationMapper;
import com.jzo2o.customer.model.domain.AgencyCertification;
import com.jzo2o.customer.model.domain.AgencyCertificationAudit;
import com.jzo2o.customer.model.domain.WorkerCertification;
import com.jzo2o.customer.model.domain.WorkerCertificationAudit;
import com.jzo2o.customer.model.dto.request.AgencyCertificationAuditAddReqDTO;
import com.jzo2o.customer.model.dto.request.AgencyCertificationAuditPageQueryReqDTO;
import com.jzo2o.customer.model.dto.request.CertificationAuditReqDTO;
import com.jzo2o.customer.model.dto.response.AgencyCertificationAuditResDTO;
import com.jzo2o.customer.model.dto.response.RejectReasonResDTO;
import com.jzo2o.customer.model.dto.response.WorkerCertificationAuditResDTO;
import com.jzo2o.customer.service.IAgencyCertificationAuditService;
import com.jzo2o.customer.service.IServeProviderService;
import com.jzo2o.mvc.utils.UserContext;
import com.jzo2o.mysql.utils.PageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * @author wjh
 * @create 2024-04-27 15:19
 */
@Service
@Slf4j
public class AgencyCertificationAuditServiceImpl extends ServiceImpl<AgencyCertificationAuditMapper, AgencyCertificationAudit> implements IAgencyCertificationAuditService {
    
    @Resource
    private AgencyCertificationMapper agencyCertificationMapper;
    @Resource
    private IServeProviderService serveProviderService;
    @Override
    public void applyCertification(AgencyCertificationAuditAddReqDTO agencyCertificationAuditAddReqDTO) {
        CurrentUserInfo currentUserInfo = UserContext.currentUser();
        AgencyCertificationAudit agencyCertificationAudit = BeanUtil.toBean(agencyCertificationAuditAddReqDTO, AgencyCertificationAudit.class);
        agencyCertificationAudit.setServeProviderId(currentUserInfo.getId());
        save(agencyCertificationAudit);
        AgencyCertification agencyCertification = agencyCertificationMapper.selectById(currentUserInfo.getId());
        if (ObjectUtil.isNotNull(agencyCertification)) {
            agencyCertification.setCertificationStatus(CertificationStatusEnum.PROGRESSING.getStatus());
            agencyCertificationMapper.updateById(agencyCertification);
        } else {
            agencyCertification = BeanUtil.toBean(agencyCertificationAuditAddReqDTO, AgencyCertification.class);
            agencyCertification.setId(currentUserInfo.getId());
            agencyCertification.setCertificationStatus(CertificationStatusEnum.PROGRESSING.getStatus());
            agencyCertificationMapper.insert(agencyCertification);
        }
    }

    @Override
    @Transactional
    public void auditCertification(Long id, CertificationAuditReqDTO certificationAuditReqDTO) {
        AgencyCertificationAudit agencyCertificationAudit = getById(id);
        AgencyCertification agencyCertification = agencyCertificationMapper.selectById(agencyCertificationAudit.getServeProviderId());
        if (ObjectUtil.isNotNull(agencyCertificationAudit)) {
            //如果是认证成功，就要设置认证成功的时间
            if (certificationAuditReqDTO.getCertificationStatus() == 2) {
                agencyCertification.setCertificationTime(LocalDateTime.now());
                //还要修改服务人员/机构人员的名称
                serveProviderService.updateNameById(agencyCertificationAudit.getServeProviderId(), agencyCertificationAudit.getName());
            } else {
                //如果是驳回就设置驳回原因
                agencyCertificationAudit.setRejectReason(certificationAuditReqDTO.getRejectReason());
            }
            //设置认证状态
            agencyCertificationAudit.setCertificationStatus(certificationAuditReqDTO.getCertificationStatus());
            //设置审核状态,已审核
            agencyCertificationAudit.setAuditStatus(1);
            //设置审核时间
            agencyCertificationAudit.setAuditTime(LocalDateTime.now());
            CurrentUserInfo currentUserInfo = UserContext.currentUser();
            //设置审核人id
            agencyCertificationAudit.setAuditorId(currentUserInfo.getId());
            //设置审核人名称
            agencyCertificationAudit.setAuditorName(currentUserInfo.getName());
            //设置认证表中的认证状态
            agencyCertification.setCertificationStatus(certificationAuditReqDTO.getCertificationStatus());
        } else {
            throw new ForbiddenOperationException("认证信息不存在");
        }
        //修改申请表中的数据
        updateById(agencyCertificationAudit);
        //修改认证表中的数据
        agencyCertificationMapper.updateById(agencyCertification);
    }

    @Override
    public PageResult<AgencyCertificationAuditResDTO> pageQuery(AgencyCertificationAuditPageQueryReqDTO agencyCertificationAuditPageQueryReqDTO) {
        Page<AgencyCertificationAudit> page = new Page<>(agencyCertificationAuditPageQueryReqDTO.getPageNo(), agencyCertificationAuditPageQueryReqDTO.getPageSize());
        String orderBy1 = agencyCertificationAuditPageQueryReqDTO.getOrderBy1();
        String orderBy2 = agencyCertificationAuditPageQueryReqDTO.getOrderBy2();

        if (StringUtils.isNotBlank(orderBy1)) {
            agencyCertificationAuditPageQueryReqDTO.setOrderBy1(StringUtils.toUnderlineCase(orderBy1));
        }
        if (StringUtils.isNotBlank(orderBy2)) {
            agencyCertificationAuditPageQueryReqDTO.setOrderBy2(StringUtils.toUnderlineCase(orderBy2));
        }
        baseMapper.pageQuery(page, agencyCertificationAuditPageQueryReqDTO);
        return PageUtils.toPage(page, AgencyCertificationAuditResDTO.class);
    }

    @Override
    public RejectReasonResDTO queryCurrentUserLastRejectReason() {
        LambdaQueryWrapper<AgencyCertificationAudit> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AgencyCertificationAudit::getServeProviderId, UserContext.currentUser().getId())
                .eq(AgencyCertificationAudit::getAuditStatus, 1)
                .eq(AgencyCertificationAudit::getCertificationStatus,3)
                .orderByDesc(AgencyCertificationAudit::getCreateTime)
                .last("limit 1");
        AgencyCertificationAudit agencyCertificationAudit = baseMapper.selectOne(queryWrapper);
        return new RejectReasonResDTO(agencyCertificationAudit.getRejectReason());
    }
}
